<template>
	<view class="main">
		<u-search bgColor="#fff" :showAction="false" placeholder="输入昵称搜索" v-model="nickname" @change="change"></u-search>
		<!-- <view class="tabs flex align between">
			<view class="flex align tab-item" @click="toShow" style="border-right: solid 1rpx #D9D9D9;">
				添加时间
				<image src="../../static/icon3.png"></image>
				<view class="tab-option" v-if="show">
					<view>充值</view>
					<view>项目退款</view>
				</view>
			</view>
			<view class="flex align tab-item" style="border-right: solid 1rpx #D9D9D9;">
				状态
				<image src="../../static/icon3.png"></image>
			</view>
			<view class="flex align tab-item">
				类型
				<image src="../../static/icon3.png"></image>
			</view>
		</view>
		<view class="option">
			<view class="option-item">抖音官方接口</view>
			<view class="option-item">快手官方接口</view>
			<view class="option-item">BILI官方接口</view>
			<view class="option-item">视频号协议授权</view>
			<view class="option-item">小红书协议授权</view>
			<view class="option-item">抖音扫码授权</view>
			<view class="option-item">抖音CK授权</view>
		</view> -->
		<view class="option" style="margin-top: 30rpx;">
			<!-- <view class="option-item">抖音官方接口</view>
			<view class="option-item">快手官方接口</view>
			<view class="option-item">BILI官方接口</view> -->
			<view class="option-item" @click="toShowPop('shipinhao')">视频号协议授权</view>
			<!-- <view class="option-item">小红书协议授权</view> -->
			<view class="option-item" @click="toShowPop('dou')">抖音扫码授权</view>
			<view class="option-item" @click="showPop2=true">抖音CK授权</view>
		</view>
		<view class="list" v-for="(item,index) in list" :key="index">
			<view class="list-info flex align">
				<view class="list-info-left">
					<image :src="item.head"></image>
					<view>正常</view>
				</view>
				<view class="list-info2">
					<view class="name">{{item.nickname}}</view>
					<view>ID:{{item.id}}</view>
					<view>粉丝数：{{item.fan}}</view>
					<view v-if="item.group.name!=null">分组名：{{item.group.name}}</view>
					<view>openid / 账号：{{item.openid}}</view>
					<view>绑定POI：{{item.poi_name}}</view>
					<view>
						类型：{{item.type==1?'抖音CK':item.type==2?'西瓜':item.type==3?'头条':item.type==4?'快手':item.type==5?'哔哩哔哩':item.type==6?'视频号':item.type==7?'小红书':item.type==8?'抖音接口':''}}
					</view>
				</view>
				<view class="list-info3">
					<view class="zan flex align">
						<image src="../../static/zan.png" mode="widthFix"></image>
						100W
					</view>
					<view>sms验证时间：{{item.sms_uptime==null?'-':item.sms_uptime}}</view>
					<view>授权到期：{{item.token_out_time}}</view>
					<view>自动续约：{{item.refresh_out_time}}</view>
				</view>
			</view>
			<view class="time">{{item.addtime}}</view>
			<!-- <view class="imgList flex align between">
				<view class="img-item">
					<image src="../../static/video/img.png"></image>
					<view>这是你的“守”</view>
				</view>
				<view class="img-item">
					<image src="../../static/video/img.png"></image>
					<view>这是你的“守”</view>
				</view>
			</view> -->
		</view>
		<uni-load-more :status="status" v-if="!empty" :content-text="loadText"></uni-load-more>
		<xw-empty :isShow="empty" text="暂无数据" textColor="#777777"></xw-empty>
		<u-popup :show="showPop" mode="center">
			<view class="showPop" v-if="douCodeInfo!=''">
				<view class="delPop" @click="close()">
					<uni-icons type="closeempty" color="#c3c3c3" size="25"></uni-icons>
				</view>
				<view class="showPopTit">扫码登录</view>
				<image :src="'data:image/png;base64,'+douCodeInfo.qrcode"></image>
				<view class="tips" v-if="checkDouInfo.description!=''">{{checkDouInfo.description}}</view>
				<view class="tips tips2" v-if="checkDouInfo.description==''">用抖音App扫码授权</view>
				<view class="popBtn" @click="getDouCode()">重新刷新</view>
			</view>
			<view class="showPop" v-if="shipinhaoCodeInfo!=''">
				<view class="delPop" @click="close2()">
					<uni-icons type="closeempty" color="#c3c3c3" size="25"></uni-icons>
				</view>
				<view class="showPopTit">扫码登录</view>
				<image :src="shipinhaoCodeInfo.image"></image>
				<view class="tips" v-if="checkShipinhaoInfo.status==5">二维码已失效，请重新刷新</view>
				<view class="tips tips2" v-if="checkShipinhaoInfo.status==0">请使用微信扫码</view>
				<view class="popBtn" @click="getShipinhaoCode()">重新刷新</view>
			</view>
		</u-popup>
		<u-popup :show="showPop2" mode="center">
			<view class="showPop">
				<view class="delPop" @click="showPop2=false">
					<uni-icons type="closeempty" color="#c3c3c3" size="25"></uni-icons>
				</view>
				<view class="showPopTit">添加</view>
				<view class="input-item">
					<view>cookie：</view>
					<textarea placeholder="请输入d因pc网页cookie" v-model="cookie"></textarea>
				</view>
				<view class="tips2">
					<view>请填写完整的cookie，获取方法请看操作文档</view>
					<view style="color: red;">*注意*：授权的D音账号需要在电脑网页的D音创作者平台发过一次视频，否则会触发手机验证码，导致发送失败</view>
					<view style="color: red;">*注意*：授权完后不要主动退出D音账号，否则导致cookie失效（允许关闭浏览器）</view>
				</view>
				<view class="popBtn" @click="toAddCookie()">确定</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		tuiguangUserIndex,
		getQrcodeDou,
		checkQrconnectDou,
		getQrcodeShipinhao,
		checkQrconnectShipinhao
	} from '@/api/apiData.js';
	export default {
		data() {
			return {
				cookie:'',
				showPop: false,
				showPop2: false,
				searchValue: '',
				show: false,
				totalPage: 1,
				page: 0,
				offset: 20,
				list: [],
				scrollTop: 0,
				empty: false,
				loadText: {
					loadmore: '点击或上拉加载更多',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				status: 'loadmore',
				douCodeInfo: '',
				timeOut: null,
				checkDouInfo: {
					description: ''
				},
				shipinhaoCodeInfo: '',
				timeOut2: null,
				checkShipinhaoInfo: {
					status: 0
				},
				nickname:''
			};
		},
		onLoad(option) {
			this.getlist();
			// console.log(decodeURIComponent('%7B%22nickname%22%3A%22%E6%B5%8B%E8%AF%95%22%7D'))
		},
		onPageScroll(e) {
			const that = this;
			that.scrollTop = e.scrollTop;
		},
		onReachBottom() {
			const that = this;
			that.getlist();
		},
		methods: {
			change(){
				const that = this;
				that.page = 0
				that.list = []
				that.getlist()
			},
			// 添加cookie
			toAddCookie(){
				let obj = {
					'row[cookie]':this.cookie
				}
				if(this.cookie==''){
					uni.showToast({
						title: '请输入cookie',
						icon:'none'
					})
					return false
				}
				let that = this
				uni.showLoading({
					title: "loading",
					mask: true
				})
				uni.request({
					url: 'https://jzxt.jiugezixun.cn/storeadmin/tuiguang_user/add?type=1&dialog=1',
					data: obj,
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'X-Requested-With': 'XMLHttpRequest',
						'Cookie': uni.getStorageSync('cookies') // 从存储中读取cookies并设置到请求头
					},
					success: result => {
						if (result.data.code == 1) {
							uni.showToast({
								title: '添加成功'
							})
							that.show2 = false
							setTimeout(() => {
								that.page = 0
								that.list = []
								that.getlist()
							}, 500)
							uni.hideLoading();
						} else {
							uni.showToast({
								title: result.data.msg,
								icon:'none'
							})
							uni.hideLoading();
						}
					},
					fail: result => {
						uni.hideLoading();
					}
				})
			},
			// 抖音
			async getDouCode() {
				clearInterval(this.timeOut)
				const result = await getQrcodeDou()
				this.douCodeInfo = result.data
				this.timeOut = setInterval(() => {
					this.getCheckDou()
				}, 5000)
			},
			async getCheckDou() {
				const result = await checkQrconnectDou({
					token: this.douCodeInfo.token
				})
				this.checkDouInfo = result
				if (result.error_code == 1) {
					clearInterval(this.timeOut)
				}
			},
			// 视频号
			async getShipinhaoCode() {
				clearInterval(this.timeOut2)
				const result = await getQrcodeShipinhao()
				this.shipinhaoCodeInfo = result.data
				this.timeOut2 = setInterval(() => {
					this.getCheckShipinhao()
				}, 5000)
			},
			async getCheckShipinhao() {
				const result = await checkQrconnectShipinhao({
					token: this.shipinhaoCodeInfo.token
				})
				this.checkShipinhaoInfo = result.data
				if (result.data.status != 0) {
					clearInterval(this.timeOut2)
				}
			},
			close() {
				this.showPop = false
				clearInterval(this.timeOut)
			},
			close2() {
				this.showPop = false
				clearInterval(this.timeOut2)
			},
			toShowPop(n) {
				this.showPop = true
				if (n == 'dou') {
					this.getDouCode()
				}
				if (n == 'shipinhao') {
					this.getShipinhaoCode()
				}
			},
			toShow() {
				this.show = !this.show
			},
			async getlist() {
				const that = this;
				const {
					totalPage,
					page,
					offset,
					list,
					group_id,
					type,
					searchValue,
					group_ids,
					nickname
				} = that;
				let obj = {
					menu_url: 'tuiguang_user',
					limit: offset,
					offset: page,
					sort: 'addtime',
					order: 'desc',
					filter:{},
					op:{},
					// op:encodeURIComponent(JSON.stringify(filter))
				};
				if(nickname!=''){
					obj.filter =JSON.stringify({'nickname':nickname})
					obj.op =JSON.stringify({'nickname':'LIKE'})
				}
				that.status = 'loading';
				if (totalPage > page) {
					let result = await tuiguangUserIndex(obj);
					that.page = page + 1;
					if (result.total == 0) {
						that.empty = true;
						that.status = 'nomore';
						return;
					}
					that.empty = false;
					setTimeout(() => {
						that.status = 'loadmore';
					}, 500);
					that.list = list.concat(result.rows);
					that.totalPage = Math.ceil(result.total / offset);
				} else {
					setTimeout(() => {
						that.status = 'nomore';
					}, 500);
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.showPop {
		width: 600rpx;
		padding-bottom: 40rpx;
		position: relative;

		.delPop {
			position: absolute;
			top: 20rpx;
			right: 20rpx;
		}

		.showPopTit {
			font-weight: bold;
			border-bottom: solid 1rpx gainsboro;
			text-align: center;
			padding: 20rpx 0;
		}
		.input-item{
			font-size: 30rpx;
			padding: 20rpx;
			textarea{
				padding: 20rpx;
				font-size: 28rpx;
				border: solid 1rpx gainsboro;
				width: 85%;
				margin-top: 20rpx;
				height: 200rpx;
			}
		}
		.tips2{
			padding:10rpx 20rpx;
			font-size: 24rpx;
			view{
				margin-bottom: 10rpx;
			}
		}

		image {
			width: 400rpx;
			height: 400rpx;
			display: block;
			margin: 30rpx auto 0;
		}

		.tips {
			text-align: center;
			color: red;
			padding: 20rpx 0;
			font-size: 28rpx;
		}

		.tips2 {
			color: black;
		}

		.popBtn {
			background: #29a1e6;
			color: white;
			font-size: 28rpx;
			padding: 20rpx 0;
			width: 400rpx;
			border-radius: 5rpx;
			text-align: center;
			margin: 0 auto;
			border-radius: 20rpx;
		}
	}

	.main {
		padding: 13rpx 30rpx;
	}

	.tabs {
		background: white;
		padding: 15rpx 0;
		margin: 14rpx 0;

		.tab-item {
			flex: 1;
			text-align: center;
			justify-content: center;
			position: relative;

			image {
				width: 22rpx;
				height: 12rpx;
				margin-left: 26rpx;
			}

			.tab-option {
				position: absolute;
				width: 100%;
				top: 60rpx;
				background: white;
				box-shadow: 1rpx 1rpx 1rpx 2rpx gainsboro;

				view {
					padding: 16rpx 0;
					border-bottom: solid 1rpx #D9D9D9;
				}
			}
		}
	}

	.option {
		display: grid;
		grid-template-columns: 1fr 1fr 1fr 1fr;
		grid-row-gap: 14rpx;
		grid-column-gap: 20rpx;

		.option-item {
			background: #2D2D2D;
			color: white;
			line-height: 48rpx;
			text-align: center;
			font-size: 20rpx;
			border-radius: 8rpx;
		}
	}

	.list {
		background: white;
		padding: 37rpx 19rpx;
		margin-top: 20rpx;
		border-radius: 10rpx;

		.list-info {
			.list-info-left {
				image {
					width: 143.57rpx;
					height: 143.57rpx;
				}

				view {
					width: 86rpx;
					line-height: 31rpx;
					color: white;
					background: #369805;
					border-radius: 20rpx;
					margin: 8rpx auto;
					text-align: center;
					font-size: 20rpx;
				}
			}

			.list-info2 {
				font-size: 18rpx;
				color: #424242;

				.name {
					font-size: 32rpx;
					font-weight: bold;
					color: #0F0F0F;
				}

				view {
					margin-bottom: 5rpx;
				}
			}

			.list-info3 {
				font-size: 18rpx;
				color: #424242;
				margin-left: 10rpx;

				.zan {
					font-size: 24rpx;
					margin-bottom: 26rpx;
					justify-content: flex-end;

					image {
						width: 29rpx;
						height: 35rpx;
						margin-right: 9rpx;
					}
				}
			}
		}

		.time {
			font-size: 16rpx;
			color: #424242;
			text-align: right;
			border-bottom: solid 1rpx #707070;
		}

		.imgList {
			margin: 16rpx 33rpx;

			.img-item {
				text-align: center;
				font-size: 24rpx;

				image {
					width: 245rpx;
					height: 137rpx;
				}
			}
		}
	}
</style>